<template>
  <div class="viewed">
    <div v-if="userInfo" class="list">
      <dl
        @click="toReader('reader', item)"
        v-for="item of list.rows"
        :key="item.id"
      >
        <dt class="cover">
          <img
            :src="item.appBook.coverUrl"
            class="coverImg"
            v-default-img="defaultImg"
          />
        </dt>
        <dd>
          <h3 class="bookName ellipsis" v-text="item.appBook.bookTitle"></h3>
          <p class="ellipsis-two bookIntro" v-html="item.appBook.bookIntro"></p>
          <div class="foot">
            <span class="record" v-text="item.cOrder"></span>
            <div>
              <span class="toPage">Continue Reading>></span>
              <el-button
                class="bookToBookCase"
                :class="{ disBtn: item.appBook.isCollect }"
                @click.stop="bookToBookCase(item.appBook)"
                v-text="
                  item.appBook.isCollect ? 'In Bookself' : 'Add To Bookself'
                "
              ></el-button>
            </div>
          </div>
        </dd>
      </dl>
      <div class="page-wrap">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="params.pageNum"
          :page-size="params.pageSize"
          hide-on-single-page
          background
          layout="prev, pager, next"
          :total="list.total"
        >
        </el-pagination>
      </div>
    </div>
    <div v-show="isEmpty" class="empty">
      <svg-icon class="empty-icon" icon-class="empty-viewed"></svg-icon>
      <p>No reading record.</p>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapGetters } from "vuex";
export default {
  data() {
    return {
      defaultImg: require("@/assets/images/default-cover.png"),
      list: {}, //阅读记录列表
      params: {
        pageNum: 1,
        pageSize: 20,
      },
      scrollTop: 0,
      isEmpty: false, //阅读记录是否为空
    };
  },
  components: {},
  computed: {
    ...mapGetters(["userInfo"]),
  },
  watch: {},
  created() {},
  mounted() {
    this.getUserReadRecord();
  },
  methods: {
    ...mapMutations({
      // setShowPopup: "SET_SHOW_POPUP",
      setCurrentDialog: "SET_CURRENT_DIALOG",
    }),
    //当前页
    handleCurrentChange(val) {
      this.params.pageNum = val;
      this.getUserReadRecord();
      this.backTop();
    },
    // 查询用户的书籍阅读记录(分页)
    async getUserReadRecord() {
      let res = await this.$api.reader.getUserReadRecord(this.params);
      if (res.code === 1) {
        this.list = res.source;
        if (res.source.total == 0) {
          this.isEmpty = true;
        }
      }
    },
    // 添加到书架
    async bookToBookCase(item) {
      if (this.userInfo) {
        if (item.isCollect) {
          let params = {
            bookIds: item.bookId, //书籍id
          };
          let res = await this.$api.reader.delBookToBookCase(params);
          if (res.code === 1) {
            this.isBookSelf = 0;
            this.$message({
              duration: "1500",
              message: "Move successful",
            });
          } else {
            this.$message({
              message: res.message ? res.message : "Fail to add",
              duration: "1500",
              type: "error",
            });
          }
        } else {
          let params = {
            bookId: item.bookId, //书籍id
          };
          let res = await this.$api.reader.addBookToBookCase(params);
          if (res.code === 1) {
            this.isBookSelf = 1;
            this.$message({
              message: "Add successful",
              duration: "1500",
              type: "success",
            });
          } else {
            this.$message({
              message: res.message ? res.message : "Fail to add",
              duration: "1500",
              type: "error",
            });
          }
        }
        this.getUserReadRecord();
      } else {
        this.setCurrentDialog({ current: "login" });
        // this.setShowPopup(true);
      }
    },
    // 跳转阅读器
    toReader(name, item) {
      if (name === "reader") {
        const { href } = this.$router.resolve({
          name: "reader",
          params: { id: item.bookId }, //书籍id
        });
        window.open(href, "_blank");
      }
    },
    // 去登录
    toLogin() {
      this.setCurrentDialog({ current: "login" });
      // this.setShowPopup(true);
    },
    // 返回顶部
    backTop() {
      let timer = setInterval(() => {
        let ispeed = Math.floor(-this.scrollTop / 5);
        document.documentElement.scrollTop = document.body.scrollTop =
          this.scrollTop + ispeed;
        if (this.scrollTop === 0) {
          clearInterval(timer);
        }
      }, 50);
    },
  },
};
</script>

<style lang="scss" scoped>
.viewed {
  > .list {
    display: flex;
    flex-direction: column;
    padding: 23px 0;
    > dl {
      width: 100%;
      height: 180px;
      margin: 16px 0;
      display: flex;
      cursor: pointer;
      dt.cover {
        margin-right: 17px;
        img.coverImg {
          width: 140px;
          height: 100%;
          box-shadow: 0px 1px 4px 1px rgba(0, 0, 0, 0.2);
          border-radius: 8px;
        }
      }
      dd {
        width: 100%;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        > h3.bookName {
          font-size: 16px;
          font-weight: 500;
          color: #000000;
          line-height: 26px;
          font-family: Poppins-Medium, Poppins;
        }
        .bookIntro {
          font-size: 14px;
          font-family: Poppins-Regular, Poppins;
          font-weight: 400;
          color: #666666;
          line-height: 23px;
        }
        .foot {
          display: flex;
          align-items: center;
          justify-content: space-between;
          .record {
            font-size: 14px;
            font-family: Poppins-Regular, Poppins;
            font-weight: 400;
            color: #999999;
            line-height: 23px;
            &::before {
              content: "Last read to chapter ";
            }
          }
          .toPage {
            font-size: 16px;
            margin-right: 22px;
            font-family: Poppins-Medium, Poppins;
            font-weight: 500;
            color: #e86472;
            line-height: 26px;
          }
          .bookToBookCase {
            width: 176px;
            height: 38px;
            font-size: 16px;
            border-width: 0;
            font-family: Poppins-Medium, Poppins;
            font-weight: 500;
            color: #ffffff;
            background: #e86472;
            &.disBtn {
              background: #cccccc;
            }
          }
        }
      }
    }
  }
  > .empty {
    text-align: center;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .empty-icon {
      width: 240px;
      height: 195px;
    }
    > p {
      margin-top: 31px;
      font-size: 14px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #999999;
      line-height: 20px;
    }
  }
}
</style>
